using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using Microsoft.DirectX.Direct3D;

namespace iDorm
{
	/// <summary>
	/// Summary description for Form1.
	/// When the user interacts with the form, these methods are called
	/// Interacting with form including clicking the add, 2d, 3d, walkthrough buttons
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		// Variables of Form
		private System.Windows.Forms.Panel target;
		private Renderer r;

		private bool mousedown = false;
		private bool in3D = false;
		private bool walkthrough = false;

		private float angle = 0.0f;

		ModelLoader ml;


		// Room
		Room room;


		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Button button1;

		// game Mode
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuItem8;
		private System.Windows.Forms.SaveFileDialog SaveDialog;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.ContextMenu contextMenu1;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.ComboBox comboBox2;
		private System.Windows.Forms.MenuItem menuItem9;
		private System.Windows.Forms.Button LoadTemplate;
		private System.Windows.Forms.ComboBox comboBox3;

		/// <summarry>
		/// Required Properties
		/// </summarry>
		private bool AppStillIdle
		{
			get
			{
				NativeMethods.Message msg;
				return !NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
			}
		}
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.target = new System.Windows.Forms.Panel();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.button1 = new System.Windows.Forms.Button();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem8 = new System.Windows.Forms.MenuItem();
			this.menuItem9 = new System.Windows.Forms.MenuItem();
			this.SaveDialog = new System.Windows.Forms.SaveFileDialog();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.contextMenu1 = new System.Windows.Forms.ContextMenu();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.button4 = new System.Windows.Forms.Button();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.comboBox2 = new System.Windows.Forms.ComboBox();
			this.LoadTemplate = new System.Windows.Forms.Button();
			this.comboBox3 = new System.Windows.Forms.ComboBox();
			this.SuspendLayout();
			// 
			// target
			// 
			this.target.BackColor = System.Drawing.Color.DimGray;
			this.target.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.target.Location = new System.Drawing.Point(8, 8);
			this.target.Name = "target";
			this.target.Size = new System.Drawing.Size(640, 640);
			this.target.TabIndex = 0;
			this.target.MouseUp += new System.Windows.Forms.MouseEventHandler(this.target_MouseUp);
			this.target.MouseMove += new System.Windows.Forms.MouseEventHandler(this.target_MouseMove);
			this.target.MouseDown += new System.Windows.Forms.MouseEventHandler(this.target_MouseDown);
			// 
			// listBox1
			// 
			this.listBox1.Items.AddRange(new object[] {
														  "Dresser",
														  "Chair",
														  "Desk",
														  "Bed",
														  "Fridge"});
			this.listBox1.Location = new System.Drawing.Point(672, 24);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(152, 82);
			this.listBox1.TabIndex = 1;
			// 
			// button1
			// 
			this.button1.ForeColor = System.Drawing.SystemColors.ControlText;
			this.button1.Location = new System.Drawing.Point(680, 288);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(128, 32);
			this.button1.TabIndex = 2;
			this.button1.Text = "Add";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem2});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem3,
																					  this.menuItem5,
																					  this.menuItem4,
																					  this.menuItem6,
																					  this.menuItem7});
			this.menuItem1.Text = "File";
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 0;
			this.menuItem3.Text = "New";
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 1;
			this.menuItem5.Text = "Open";
			this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 2;
			this.menuItem4.Text = "Save";
			this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 3;
			this.menuItem6.Text = "-";
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 4;
			this.menuItem7.Text = "Exit";
			this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem8,
																					  this.menuItem9});
			this.menuItem2.Text = "Help";
			// 
			// menuItem8
			// 
			this.menuItem8.Index = 0;
			this.menuItem8.Text = "Documentation";
			// 
			// menuItem9
			// 
			this.menuItem9.Index = 1;
			this.menuItem9.Text = "Release Notes";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(680, 336);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(128, 32);
			this.button2.TabIndex = 3;
			this.button2.Text = "3D";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(680, 384);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(128, 32);
			this.button3.TabIndex = 4;
			this.button3.Text = "2D";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// contextMenu1
			// 
			this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup_1);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(680, 432);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(128, 32);
			this.button4.TabIndex = 5;
			this.button4.Text = "Wakthrough";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// comboBox1
			// 
			this.comboBox1.Items.AddRange(new object[] {
														   "BARH",
														   "Barton",
														   "Bray",
														   "Cary",
														   "Colonie",
														   "Crockett",
														   "Davison",
														   "E-Complex",
														   "Hall",
														   "Nason",
														   "North",
														   "Nugent",
														   "Quad",
														   "Sharp",
														   "RAHP",
														   "Stacwyck",
														   "Warren"});
			this.comboBox1.Location = new System.Drawing.Point(680, 160);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(120, 21);
			this.comboBox1.TabIndex = 6;
			this.comboBox1.Text = "Select Dorm Room";
			this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
			// 
			// comboBox2
			// 
			this.comboBox2.Items.AddRange(new object[] {
														   "Single",
														   "Double"});
			this.comboBox2.Location = new System.Drawing.Point(680, 200);
			this.comboBox2.Name = "comboBox2";
			this.comboBox2.Size = new System.Drawing.Size(121, 21);
			this.comboBox2.TabIndex = 7;
			this.comboBox2.Text = "Select Dorm Type";
			this.comboBox2.Visible = false;
			this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
			// 
			// LoadTemplate
			// 
			this.LoadTemplate.Location = new System.Drawing.Point(680, 240);
			this.LoadTemplate.Name = "LoadTemplate";
			this.LoadTemplate.Size = new System.Drawing.Size(128, 32);
			this.LoadTemplate.TabIndex = 8;
			this.LoadTemplate.Text = "Load Template";
			this.LoadTemplate.Visible = false;
			this.LoadTemplate.Click += new System.EventHandler(this.button5_Click);
			// 
			// comboBox3
			// 
			this.comboBox3.Items.AddRange(new object[] {
														   "Blue Carpet",
														   "Dirty Carpet",
														   "Tiger Carpet",
														   "Leopard Carpet",
														   "Zebra Carpet",
														   "Bathroom Tiles",
														   "Hardwood"});
			this.comboBox3.Location = new System.Drawing.Point(680, 120);
			this.comboBox3.Name = "comboBox3";
			this.comboBox3.Size = new System.Drawing.Size(121, 21);
			this.comboBox3.TabIndex = 9;
			this.comboBox3.Text = "Select Flooring";
			this.comboBox3.SelectedIndexChanged += new System.EventHandler(this.comboBox3_SelectedIndexChanged);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(848, 589);
			this.Controls.Add(this.comboBox3);
			this.Controls.Add(this.LoadTemplate);
			this.Controls.Add(this.comboBox2);
			this.Controls.Add(this.comboBox1);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.target);
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "iDorm";
			this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.target_KeyPress);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion



		// onAppIdle
		//
		// Description:	Called every time the application has a free second
		// Input:		Object - not important
		//				Event args - not used
		// Output:		None
		public void onAppIdle(object sender, EventArgs e)
		{

			while (AppStillIdle)
			{
				if (walkthrough)
				{
					angle += .01f;
					r.RotateAroundCenter(angle);
				}

				r.BeginRender(Color.Black);
			{
				room.RenderRoom(r.GetDevice());

				if (!in3D)
				{
					ArrayList objects = room.GetObjects();

					foreach (Object o in objects)
					{
						int x = (int) (o.GetX() * 40.0f) + 320;
						int y = (int) (320 - o.GetZ() * 40.0f);
						int length = (int) (o.GetLength() * 40.0f);
						int width = (int) (o.GetWidth() * 40.0f);

						r.DrawTextCentered(o.GetName(), x, y, length, width, 0xFF000000);
					}
				}
			}
				r.EndRender();
			}
		}
		
		//Loads room upon startup

		private void Form1_Load(object sender, System.EventArgs e)
		{
			r = new Renderer(target);
			r.Switch2D();

			room = new Room(12, 9, r.GetDevice());
			
			ml = new ModelLoader();

			room.AttachFloor(ml.LoadModel(r.GetDevice(), "Floor.x", "ConcreteWall.jpg", System.Drawing.Color.White.ToArgb()));
			room.AttachWalls(ml.LoadModel(r.GetDevice(), "RoomWithHole.x", "ConcreteWall.jpg", System.Drawing.Color.White.ToArgb()));
		}
		/// <summary>
		/// Adds model when add is pressed on form
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void button1_Click(object sender, System.EventArgs e)
		{
			Model m;
			
			Object obj;

			if (listBox1.SelectedIndex == 0)
			{
				obj = new Object("Dresser", 0,0,0, 3.0f, 2.0f, 5.0f);
				m = ml.LoadModel(r.GetDevice(), "Dresser.x", "Wood.jpg", System.Drawing.Color.White.ToArgb());
				obj.AttachModel(m);
			}
			else if (listBox1.SelectedIndex == 1)
			{
				obj = new Object("Chair", 0,0,0, 1.5f, 1.5f, 3.0f);
				m = ml.LoadModel(r.GetDevice(), "Chair.x", "Wood.jpg", System.Drawing.Color.White.ToArgb());
				obj.AttachModel(m);
			}
			else if (listBox1.SelectedIndex == 2)
			{
				obj = new Object("Desk", 0,0,0, 4.5f, 3.0f, 3.0f);
				m = ml.LoadModel(r.GetDevice(), "Desk.x", "Wood.jpg", System.Drawing.Color.White.ToArgb());
				obj.AttachModel(m);
			}
			else if (listBox1.SelectedIndex == 3) ///If bed
			{
				obj = new Object("Bed", 0,0,0, 6.5f, 3.5f, 3.5f);
				m = ml.LoadModel(r.GetDevice(), "Bed.x", "Wood.jpg", System.Drawing.Color.White.ToArgb());
				obj.AttachModel(m);
			}
			else //if fridge
			{
				obj = new Object("Fridge", 0,0,0, 10.0f, 10.0f, 10.0f);
				m = ml.LoadModel(r.GetDevice(), "Fridge.x", "", System.Drawing.Color.White.ToArgb());
				obj.AttachModel(m);
			}
			room.AddObject(obj);
		}
		/// <summary>
		/// When the mouse is pressed down, this method determines whether the points
		/// of the mouse intersect with the objects. if it does, it selects the object
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void target_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (e.Button == System.Windows.Forms.MouseButtons.Left)
			{
				ArrayList objects = room.GetObjects();

				float highest = 0.0f;
				bool objectIntersected = false;
				int counter = 0;
				int index = 0;

				foreach (Object o in objects)
				{
					o.selected = false;
					if (o.Intersects(new Point(e.X - 320, 320 - e.Y)))	
					{
						if (o.GetY() + o.GetHeight() > highest)
						{
							objectIntersected = true;
							index = counter;
							highest = o.GetY() + o.GetHeight();
						}
					}
					counter++;
				}

				if (objectIntersected)
				{
					((Object)objects[index]).selected = true;
					((Object)objects[index]).moving = true;
				}
				mousedown = true;
			}
		}
		/// <summary>
		/// When the mouse is pressed and let go, this will determine if any object is needed to be 
		/// moved or if the right button is pressed and let go, a context menu pops up
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void target_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (e.Button == System.Windows.Forms.MouseButtons.Left)
			{
				ArrayList objects = room.GetObjects();

				foreach (Object o in objects)
				{
					o.moving = false;
					mousedown = false;
				}

				// ensure no objects are floating in space...
				DropObjects();
			}

			if (e.Button == System.Windows.Forms.MouseButtons.Right)
			{
				ArrayList objects = room.GetObjects();
				bool found = false;

				foreach (Object o in objects)
				{
					if (o.Intersects(new Point(e.X - 320, 320 - e.Y)))
					{
						o.selected = true;
						found = true;
					}
				}

				if (found)
				{
					contextMenu1.Show(target, new Point(e.X, e.Y));
				}
			}
		}

		/// <summary>
		/// Checks if any object is floating in space, and if so, fixes it
		/// </summary>
		
		void DropObjects()
		{
			ArrayList objects = room.GetObjects();
			objects.Sort();

			foreach (Object o in objects)
			{
				if (o.GetY() > 0.0f)
				{
					bool floating = true;
					foreach(Object a in objects)
					{
						if (o.Equals(a))
							continue;

						if (o.Intersects(a) && (o.GetY() == a.GetY()+a.GetHeight()))
						{
							floating = false;
							break;
						}
					}

					if (floating)
					{
						float offsetY = 0.0f;
						foreach (Object b in objects)
						{
							if (o.Intersects(b) && (o.GetY() > b.GetY()) && (b.GetY() + b.GetHeight() > offsetY))
							{
								offsetY = b.GetY() + b.GetHeight();
							}
						}

						o.Move(o.GetX(), offsetY, o.GetZ(), room.GetWidth(), room.GetLength());
					}
				}
			}
		}








		/// <summary>
		/// If the mouse is being moved, it will move the object if it needs to be done
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void target_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (mousedown)
			{
				ArrayList objects = room.GetObjects();

				foreach (Object o in objects)
				{
					if (o.moving)
					{
						float newx = (e.X - 320) / 40.0f;
						float newz = (320 - e.Y) / 40.0f;

						float offsetY = 0.0f;
						foreach (Object a in objects)
						{
							if (o.Equals(a))
								continue;

							if (o.Intersects(a))
							{
								if (a.GetY() + a.GetHeight() > offsetY)
								{
									offsetY = a.GetY() + a.GetHeight();
								}
							}
						}

						o.Move(newx, offsetY, newz, room.GetWidth(), room.GetLength());
					}
				}
			}
		}
		/// <summary>
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void target_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			this.Text = "" + e.KeyChar;
		}
		/// <summary>
		///  This method switches to 3d
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void button2_Click(object sender, System.EventArgs e)
		{
			r.Switch3D();
			in3D = true;
			walkthrough = false;
		}
		/// <summary>
		///  This method switches to 2d
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button3_Click(object sender, System.EventArgs e)
		{
			r.Switch2D();
			in3D = false;
			walkthrough = false;
		}
		// This method closes the file if exit is clicked
		private void menuItem7_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
		//This has the features of the context menu pop up
		private void contextMenu1_Popup_1(object sender, System.EventArgs e)
		{
			contextMenu1.MenuItems.Clear();
			contextMenu1.MenuItems.Add("Delete", new System.EventHandler(this.contextMenu_Delete));
			contextMenu1.MenuItems.Add("Rotate Clockwise", new System.EventHandler(this.contextMenu_RotateClockwise));
			contextMenu1.MenuItems.Add("Rotate Counter-Clockwise", new System.EventHandler(this.contextMenu_RotateCounterClockwise));
		}
		/// <summary>
		/// if delete is pressed in the context menu, this function deltes the object
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void contextMenu_Delete(object sender, System.EventArgs e)
		{
			ArrayList objects = room.GetObjects();

			foreach (Object o in objects)
			{
				if (o.selected)
				{
					room.RemoveObject(o);
					break;
				}
			}
		}
		/// <summary>
		/// This method allows the user to rotate the object clockwise
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void contextMenu_RotateClockwise(object sender, System.EventArgs e)
		{
			ArrayList objects = room.GetObjects();
			foreach (Object o in objects)
			{
				if (o.selected)
				{
					o.RotateCW((float)Math.PI / 2);
				}
			}			
		}
		/// <summary>
		/// This method allows the user to rotate counter clockwise
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void contextMenu_RotateCounterClockwise(object sender, System.EventArgs e)
		{
			ArrayList objects = room.GetObjects();
			foreach (Object o in objects)
			{
				if (o.selected)
				{
					o.RotateCCW((float)Math.PI / 2);
				}
			}
		}
		/// <summary>
		/// Allows user to load a file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuItem5_Click(object sender, System.EventArgs e)
		{
			if (! (openFileDialog1.ShowDialog() == DialogResult.OK))
			{
				return;
			}

			// Load the room data
			string file = openFileDialog1.FileName;
			System.IO.StreamReader input = new System.IO.StreamReader(file);
			
			string roomName = input.ReadLine();
			float roomWidth = Convert.ToSingle(input.ReadLine());
			float roomLength = Convert.ToSingle(input.ReadLine());

			// Create the room abject
			room = new Room(roomLength, roomWidth, r.GetDevice());
			room.AttachFloor(ml.LoadModel(r.GetDevice(), "Floor.x", "WhiteCarpet.jpg", System.Drawing.Color.White.ToArgb()));
			room.AttachWalls(ml.LoadModel(r.GetDevice(), "RoomWithHole.x", "ConcreteWall.jpg", System.Drawing.Color.White.ToArgb()));
			// Load the Object data

			string objectFile = input.ReadLine();
			while (objectFile != null && objectFile != "")
			{
				string modelFile = input.ReadLine();
				string texturefile = input.ReadLine();
				float x = Convert.ToSingle(input.ReadLine());
				float y = Convert.ToSingle(input.ReadLine());
				float z = Convert.ToSingle(input.ReadLine());
				float length = Convert.ToSingle(input.ReadLine());
				float width = Convert.ToSingle(input.ReadLine());
				float height = Convert.ToSingle(input.ReadLine());
				float angle = Convert.ToSingle(input.ReadLine());
				// Create object
				Object o = new Object(objectFile, x, y, z, length, width, height);
				o.setAngle(angle);
				o.AttachModel(ml.LoadModel(r.GetDevice(), modelFile, texturefile, -1));
				room.AddObject(o);

				objectFile = input.ReadLine();
			}



			input.Close();
		}
		/// <summary>
		/// Method allows user to save room based on multiple things
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			if (! (SaveDialog.ShowDialog() == DialogResult.OK))
			{
				return;
			}

			string file = SaveDialog.FileName;
			System.IO.StreamWriter output = System.IO.File.CreateText(file);

			output.WriteLine("Room");
			output.WriteLine(room.GetWidth());
			output.WriteLine(room.GetLength());
			

			ArrayList objects = room.GetObjects();
			foreach (Object o in objects)
			{
				output.WriteLine(o.GetName());
				output.WriteLine(o.GetName() + ".X");
				output.WriteLine(o.GetModel().GetTextureFile());
				output.WriteLine(o.GetX());
				output.WriteLine(o.GetY());
				output.WriteLine(o.GetZ());
				output.WriteLine(o.GetLength());
				output.WriteLine(o.GetWidth());
				output.WriteLine(o.GetHeight());	
				output.WriteLine(o.GetAngle());
			}

			output.Close();
		}
		/// <summary>
		/// User selects Walkthrough and sees a rotation of room
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button4_Click(object sender, System.EventArgs e)
		{
			// Walkthrough

			// Ensure we are in 3D
			r.Switch3D();
			in3D = true;

			angle =  5.0f * (float)Math.PI / 4.0f;

			walkthrough = !walkthrough;
		}
		/// <summary>
		/// User selects the type of dorm room they want (Ex. Barton, Bray, Hall ...etc)
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			comboBox2.Visible=true;
			
			// if dorm room has been changed and dorm room type has been set
			if (comboBox2.SelectedIndex>=0)
			{
				comboBox2_SelectedIndexChanged(sender, e);
			}
		}
		/// <summary>
		/// User chooses the type of dorm they want (single, double, triple)
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void comboBox2_SelectedIndexChanged(object sender, System.EventArgs e)
		{
				
			/* Selected index Guide FOR COMBO BOX 1
			 * 0 = BARH - 
			 * 1 = BARTON  
			 * 2 = BRAY - 9.5x12 15.6x12
			 * 3 = CARY	- 9.5x12 15.6x12
			 * 4 = COLONIE	
			 * 5 = CROCKETT - 9.5x12 15.6x12
			 * 6 = DAVISON	
			 * 7 = ECOMPLEX	
			 * 8 = HALL - 9.5x12 15.6x12
			 * 9 = NASON	
			 * 10 = NORTH	
			 * 11 = NUGENT
			 * 12 = QUAD	
			 * 13 = SHARP	
			 * 14 = RAHP
			 * 15 STACWYCK	
			 * 16 = WARREN
			*/
			// if bray, cary, crockett, hall
			if ((comboBox1.SelectedIndex == 2)||(comboBox1.SelectedIndex == 3)||
				(comboBox1.SelectedIndex == 5)||(comboBox1.SelectedIndex == 8)||
				(comboBox1.SelectedIndex==9))
			{
				//single
				if (comboBox2.SelectedIndex == 0)
				{	room.ChangeRoomSize(9.5f, 12f);	}
					//double
				else if (comboBox2.SelectedIndex ==1) 
				{ room.ChangeRoomSize(15.6f,12f); }
			}
			// BARH
			if (comboBox1.SelectedIndex == 0)
			{
				if (comboBox2.SelectedIndex == 0)
				{	room.ChangeRoomSize(15f, 12f);	}
				else if (comboBox2.SelectedIndex ==1) 
				{ room.ChangeRoomSize(16f,15f); }
				
			}
			//Barton
			if (comboBox1.SelectedIndex == 1)
			{
				if (comboBox2.SelectedIndex == 0)
				{	room.ChangeRoomSize(13.3f, 16f);	}
				else if (comboBox2.SelectedIndex ==1) 
				{ room.ChangeRoomSize(13.75f,16f); }
			}
			
			//North Hall
			if (comboBox1.SelectedIndex == 10)
			{
				if (comboBox2.SelectedIndex == 0)
				{	room.ChangeRoomSize(7.5f, 13f);	}
				else if (comboBox2.SelectedIndex ==1) 
				{ room.ChangeRoomSize(15.2f,13f); }
			}
			//
			if ((comboBox1.SelectedIndex == 13)||(comboBox1.SelectedIndex == 14))
			{
				if (comboBox2.SelectedIndex == 0)
				{	room.ChangeRoomSize(8f, 12f);	}
			}
			if (comboBox1.SelectedIndex == 15)
			{
				if (comboBox2.SelectedIndex == 0)
				{	room.ChangeRoomSize(8f, 11f);	}
			}
			LoadTemplate.Visible=true;
		}
		
		/// <summary>
		/// Loads template based on characteristics of room (Dorm Room and Dorm Type)
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void button5_Click(object sender, System.EventArgs e)
		{
			
			///Standard Freshman Hill Dorm
			if ((comboBox1.SelectedIndex == 2)||(comboBox1.SelectedIndex == 3)||
				(comboBox1.SelectedIndex == 5)||(comboBox1.SelectedIndex == 8)||
				(comboBox1.SelectedIndex==9))
			{
				//single
				if (comboBox2.SelectedIndex == 0)
				{	
					loadTemplateFile("Resources/FreshmanHillSingle.iDorm");
				}
					//double
				else if (comboBox2.SelectedIndex ==1) 
				{ 
					loadTemplateFile("Resources/FreshmanHillDouble.iDorm");
				}
			}
		}
		
	

		private void loadTemplateFile(string file)
		{
			System.IO.StreamReader input = new System.IO.StreamReader(file);
			
			string roomName = input.ReadLine();
			float roomWidth = Convert.ToSingle(input.ReadLine());
			float roomLength = Convert.ToSingle(input.ReadLine());

			// Create the room abject
			room = new Room(roomLength, roomWidth, r.GetDevice());
			room.AttachFloor(ml.LoadModel(r.GetDevice(), "Floor.x", "WhiteCarpet.jpg", System.Drawing.Color.White.ToArgb()));
			room.AttachWalls(ml.LoadModel(r.GetDevice(), "RoomWithHole.x", "ConcreteWall.jpg", System.Drawing.Color.White.ToArgb()));
			
			// Load the Object data

			string objectFile = input.ReadLine();
			while (objectFile != null && objectFile != "")
			{
				string modelFile = input.ReadLine();
				string texturefile = input.ReadLine();
				float x = Convert.ToSingle(input.ReadLine());
				float y = Convert.ToSingle(input.ReadLine());
				float z = Convert.ToSingle(input.ReadLine());
				float length = Convert.ToSingle(input.ReadLine());
				float width = Convert.ToSingle(input.ReadLine());
				float height = Convert.ToSingle(input.ReadLine());
				float angle = Convert.ToSingle(input.ReadLine());
				// Create object
				Object o = new Object(objectFile, x, y, z, length, width, height);
				o.setAngle(angle);
				o.AttachModel(ml.LoadModel(r.GetDevice(), modelFile, texturefile, -1));
				room.AddObject(o);

				objectFile = input.ReadLine();
			}
			input.Close();
		
		}

		private void comboBox3_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (comboBox3.SelectedIndex == 0)
			{
				room.AttachFloor(ml.LoadModel(r.GetDevice(), "Floor.x", "BlueCarpet.jpg", System.Drawing.Color.White.ToArgb()));
			}
			if (comboBox3.SelectedIndex == 1)
			{
				room.AttachFloor(ml.LoadModel(r.GetDevice(), "Floor.x", "WhiteCarpet.jpg", System.Drawing.Color.White.ToArgb()));
			}
			if (comboBox3.SelectedIndex == 2)
			{
				room.AttachFloor(ml.LoadModel(r.GetDevice(), "Floor.x", "TigerCarpet.jpg", System.Drawing.Color.White.ToArgb()));
			}
			if (comboBox3.SelectedIndex == 3)		
			{
				room.AttachFloor(ml.LoadModel(r.GetDevice(), "Floor.x", "LeopardCarpet.jpg", System.Drawing.Color.White.ToArgb()));
			}
			if (comboBox3.SelectedIndex == 4)
			{
				room.AttachFloor(ml.LoadModel(r.GetDevice(), "Floor.x", "ZebraCarpet.jpg", System.Drawing.Color.White.ToArgb()));
			}
			if (comboBox3.SelectedIndex == 5)
			{
				room.AttachFloor(ml.LoadModel(r.GetDevice(), "Floor.x", "BathroomTiles.jpg", System.Drawing.Color.White.ToArgb()));
			}
			if (comboBox3.SelectedIndex == 6)
			{
				room.AttachFloor(ml.LoadModel(r.GetDevice(), "Floor.x", "HardWoodFloor.jpg", System.Drawing.Color.White.ToArgb()));
			}
		}

	}
}

